//
//  KKCommunicationController.m
//  DoMi
//
//  Created by 康凯 on 16/10/26.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKCommunicationController.h"
#import "UIColor+Hex.h"
#import "KKArticalVC.h"
#import "KKQuestionVC.h"
#import "KKAdviceVC.h"
#import "KKRankController.h"
#import "KKSearchVC.h"

@interface KKCommunicationController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *articalItem;
@property (weak, nonatomic) IBOutlet UIButton *questionItem;
@property (weak, nonatomic) IBOutlet UIButton *adviceItem;

@property (weak, nonatomic) KKArticalVC *articalView;
@property (weak, nonatomic) KKQuestionVC *questionView;
@property (weak, nonatomic) KKAdviceVC *adviceView;

@property (weak, nonatomic) UIViewController *currentVC;

@end

@implementation KKCommunicationController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //禁止透明
    self.navigationController.navigationBar.translucent=NO;
    //电量颜色设置
    [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    //背景颜色
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithHexString:@"#202021"];
    //字体颜色
    self.navigationController.navigationBar.titleTextAttributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil];
    //返回字体颜色
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化nav
    [self initNav];
    //初始化view
    [self initView];
}

#pragma mark ---初始化nav
-(void)initNav{
    UIImageView *topTitle=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelg"]];
    self.navigationItem.titleView=topTitle;
    //左侧排行榜
    UIButton *leftBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [leftBtn setImage:[UIImage imageNamed:@"rank"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(rank) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    //右侧搜索
    UIButton *rightBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [rightBtn setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
}

#pragma mark ---初始化view
-(void)initView{
    KKArticalVC *articalView=[[KKArticalVC alloc]initWithNibName:@"KKArticalVC" bundle:nil];
    self.articalView=articalView;
    KKQuestionVC *questionView=[[KKQuestionVC alloc]initWithNibName:@"KKQuestionVC" bundle:nil];
    self.questionView=questionView;
    KKAdviceVC *adviceView=[[KKAdviceVC alloc]initWithNibName:@"KKAdviceVC" bundle:nil];
    self.adviceView=adviceView;
    
    [self addChildViewController:articalView];
    [self addChildViewController:questionView];
    [self addChildViewController:adviceView];
    
    self.currentVC=articalView;
    self.articalItem.selected=YES;
    //调整子视图控制器的Frame已适应容器View
    [self fitFrameForChildViewController:self.articalView];
    //设置默认显示在容器View的内容
    [self.contentView addSubview:self.articalView.view];
}

- (void)fitFrameForChildViewController:(UIViewController *)chileViewController{
    CGRect frame = self.contentView.frame;
    frame.origin.y = 0;
    chileViewController.view.frame = frame;
}

//转换子视图控制器
- (void)transitionFromOldViewController:(UIViewController *)oldViewController toNewViewController:(UIViewController *)newViewController{
    [self transitionFromViewController:oldViewController toViewController:newViewController duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newViewController didMoveToParentViewController:self];
            _currentVC = newViewController;
        }else{
            _currentVC = oldViewController;
        }
    }];
}

#pragma mark 排行榜按钮
-(void)rank{
    KKRankController *rank=[[KKRankController alloc] init];
    [self.navigationController pushViewController:rank animated:YES];
}

#pragma mark 搜索按钮
-(void)search{
    KKSearchVC *search=[[KKSearchVC alloc] initWithNibName:@"KKSearchVC" bundle:nil];
    [self.navigationController pushViewController:search animated:YES];
}

- (IBAction)articalBtn:(id)sender {
    self.articalItem.selected=YES;
    self.questionItem.selected=NO;
    self.adviceItem.selected=NO;
    if(self.currentVC==self.articalView){
        return;
    }
    [self fitFrameForChildViewController:self.articalView];
    [self transitionFromOldViewController:_currentVC toNewViewController:self.articalView];
}
- (IBAction)questionBtn:(id)sender {
    self.articalItem.selected=NO;
    self.questionItem.selected=YES;
    self.adviceItem.selected=NO;
    if(self.currentVC==self.questionView){
        return;
    }
    [self fitFrameForChildViewController:self.questionView];
    [self transitionFromOldViewController:_currentVC toNewViewController:self.questionView];
}
- (IBAction)adviceBtn:(id)sender {
    self.articalItem.selected=NO;
    self.questionItem.selected=NO;
    self.adviceItem.selected=YES;
    if(self.currentVC==self.adviceView){
        return;
    }
    [self fitFrameForChildViewController:self.adviceView];
    [self transitionFromOldViewController:_currentVC toNewViewController:self.adviceView];
}


@end
